/**
 * TCC BSI 2013
 * xlearning.com.br
 */
package br.com.xlearning.mbean.login;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import br.com.xlearning.enumeracao.Permissao;
import br.com.xlearning.mbean.navegacao.ConstantsNavigation;

/**
 * @author Jesiel Viana
 * 
 *         Date: 04/10/2013
 */
public class XlearningUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

	private static Logger logger = Logger.getLogger(XlearningUrlAuthenticationSuccessHandler.class);
	private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException
	{
		handle(request, response, authentication);
		clearAuthenticationAttributes(request);
	}

	protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException
	{
		String targetUrl = determineTargetUrl(authentication);

		if (response.isCommitted())
		{
			logger.debug("Response has already been committed. Unable to redirect to " + targetUrl);
			return;
		}

		redirectStrategy.sendRedirect(request, response, targetUrl);
	}

	/**
	 * Builds the target URL according to the logic defined in the main class
	 * Javadoc.
	 */
	protected String determineTargetUrl(Authentication authentication)
	{
		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
		for (GrantedAuthority grantedAuthority : authorities)
		{
			if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")
					|| grantedAuthority.getAuthority().equals(Permissao.ROLE_COORDENADOR.toString()))
			{
				return ConstantsNavigation.PAGINA_INICIAL_ROLE_ADMINISTRATIVO;
			}
			else
				if (grantedAuthority.getAuthority().equals("ROLE_ALUNO"))
				{
					return ConstantsNavigation.PAGINA_INICIAL_ROLE_ALUNO;
				}
				else
					if (grantedAuthority.getAuthority().equals("ROLE_PROFESSOR"))
					{
						return ConstantsNavigation.PAGINA_INICIAL_ROLE_PROFESSOR;
					}
		}
		return ConstantsNavigation.PAGINA_INICIAL;
	}

	protected void clearAuthenticationAttributes(HttpServletRequest request)
	{
		HttpSession session = request.getSession(false);
		if (session == null)
		{
			return;
		}
		session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
	}

	public void setRedirectStrategy(RedirectStrategy redirectStrategy)
	{
		this.redirectStrategy = redirectStrategy;
	}

	protected RedirectStrategy getRedirectStrategy()
	{
		return redirectStrategy;
	}
}